﻿using DataModels.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ReportingManagement.Components.Queries
{
    /// <summary>
    /// Models the QuaryParameter entity from the database
    /// </summary>
    public class QueryParameterValue
    {
        public string Name { get; set; }
        public string Description { get; set; }
        public string ValidationExpression { get; set; }
        public bool IsValid { get; set; }
        public string Value { get; set; }
    }

    /// <summary>
    /// Interaction logic for ReportParameterCollector.xaml
    /// </summary>
    public partial class ReportParameterCollector : Window
    {
        private List<QueryParameter> queryParameters;
        private ObservableCollection<QueryParameterValue> queryParameterValues;

        private Dictionary<string, string> initialQueryParameterValues = null;
        public ReportParameterCollector()
        {
            InitializeComponent();
        }

        public ReportParameterCollector(List<QueryParameter> queryParameters, 
            Dictionary<string, string> initialQueryParameterValues = null)
        {
            InitializeComponent();

            this.queryParameters = queryParameters;
            this.initialQueryParameterValues = initialQueryParameterValues;
            LoadQueryParameters();
        }

        private void LoadQueryParameters()
        {
            // If there is no parameter then disable OK button
            if (queryParameters == null)
            {
                btnOK.IsEnabled = false;
                return;
            }
            else
            {
                if (queryParameters.Count < 1)
                {
                    btnOK.IsEnabled = false;
                    return;
                }
                else
                {
                    // Here we are going to remove sample parameters in the control.
                    // These were used for designing.  
                    gridParameterCollector.Visibility = Visibility.Collapsed; 

                    queryParameterValues = new ObservableCollection<QueryParameterValue>();
                    for (int i = 0; i < this.queryParameters.Count; i++)
                    {
                        QueryParameter parameter = this.queryParameters[i];
                        var model = new QueryParameterValue
                        {
                            Name = parameter.Name,
                            Description = parameter.Description,
                            ValidationExpression = parameter.ValidationExpression,
                            IsValid = false
                        };

                        if (initialQueryParameterValues != null)
                        {
                            if (initialQueryParameterValues.ContainsKey(parameter.Name))
                            {
                                model.Value = initialQueryParameterValues[parameter.Name];
                            }
                        }

                        queryParameterValues.Add(model);
                    }
                    lstParameters.ItemsSource = queryParameterValues;
                }
            }
        }

        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            foreach (var model in queryParameterValues)
            {
                if (!model.IsValid)
                {
                    WindowInteropHelper helper = new WindowInteropHelper(this);
                    MessageBox.Show(this,
                        string.Format("The value of the parameter '{0}' is not valid", model.Name),
                        "Report Parameter Validity");
                    return;
                }
            }

            this.DialogResult = true;
            this.Close();
        }

        public ObservableCollection<QueryParameterValue> ParameterValues
        {
            get
            {
                return this.queryParameterValues;
            }
        }
    }
}
